package com.lenovo.translatetext.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Log;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: TranslateText
 * @Package: com.lenovo.translatetext
 * @ClassName: Utils
 * @Description: Utils类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/4/22 9:44
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/4/22 9:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class Utils {
    private static final String TAG = Utils.class.getSimpleName();

    private static Map<String, String> googleLanguage = null;
    /**
     * 获取Google语言列表
     * @return
     */
    public static Map<String, String> getGoogleLanguageList() {
        /**
         * Key -> Value 语言 -> 单词表示
         */
        if(googleLanguage==null){
            synchronized (Utils.class){
                if(googleLanguage==null){
                    googleLanguage=new HashMap<>();
                    googleLanguage.put("阿尔巴尼亚语", "sq");
                    googleLanguage.put("阿拉伯语", "ar");
                    googleLanguage.put("阿塞拜疆语", "az");
                    googleLanguage.put("爱尔兰语", "ga");
                    googleLanguage.put("爱沙尼亚语", "et");
                    googleLanguage.put("巴斯克语", "eu");
                    googleLanguage.put("白俄罗斯语", "be");
                    googleLanguage.put("保加利亚语", "bg");
                    googleLanguage.put("冰岛语", "is");
                    googleLanguage.put("波兰语", "pl");
                    googleLanguage.put("波斯语", "fa");
                    googleLanguage.put("布尔语", "af");
                    googleLanguage.put("南非荷兰语", "af");
                    googleLanguage.put("丹麦语", "da");
                    googleLanguage.put("德语", "de");
                    googleLanguage.put("俄语", "ru");
                    googleLanguage.put("法语", "fr");
                    googleLanguage.put("菲律宾语", "tl");
                    googleLanguage.put("芬兰语", "fi");
                    googleLanguage.put("格鲁吉亚语", "ka");
                    googleLanguage.put("古吉拉特语", "gu");
                    googleLanguage.put("海地克里奥尔语", "ht");
                    googleLanguage.put("韩语", "ko");
                    googleLanguage.put("荷兰语", "nl");
                    googleLanguage.put("加利西亚语", "gl");
                    googleLanguage.put("加泰罗尼亚语", "ca");
                    googleLanguage.put("捷克语", "cs");
                    googleLanguage.put("卡纳达语", "kn");
                    googleLanguage.put("克罗地亚语", "hr");
                    googleLanguage.put("拉丁语", "la");
                    googleLanguage.put("拉脱维亚语", "lv");
                    googleLanguage.put("老挝语", "lo");
                    googleLanguage.put("立陶宛语", "lt");
                    googleLanguage.put("罗马尼亚语", "ro");
                    googleLanguage.put("马耳他语", "mt");
                    googleLanguage.put("马来语", "ms");
                    googleLanguage.put("马其顿语", "mk");
                    googleLanguage.put("孟加拉语", "bn");
                    googleLanguage.put("挪威语", "no");
                    googleLanguage.put("葡萄牙语", "pt");
                    googleLanguage.put("日语", "ja");
                    googleLanguage.put("瑞典语", "sv");
                    googleLanguage.put("塞尔维亚语", "sr");
                    googleLanguage.put("世界语", "eo");
                    googleLanguage.put("斯洛伐克语", "sk");
                    googleLanguage.put("斯洛文尼亚语", "sl");
                    googleLanguage.put("斯瓦希里语", "sw");
                    googleLanguage.put("泰卢固语", "te");
                    googleLanguage.put("泰米尔语", "ta");
                    googleLanguage.put("泰语", "th");
                    googleLanguage.put("土耳其语", "tr");
                    googleLanguage.put("威尔士语", "cy");
                    googleLanguage.put("乌尔都语", "ur");
                    googleLanguage.put("乌克兰语", "uk");
                    googleLanguage.put("希伯来语", "iw");
                    googleLanguage.put("希腊语", "el");
                    googleLanguage.put("西班牙语", "es");
                    googleLanguage.put("匈牙利语", "hu");
                    googleLanguage.put("亚美尼亚语", "hy");
                    googleLanguage.put("意大利语", "it");
                    googleLanguage.put("意第绪语", "yi");
                    googleLanguage.put("印地语", "hi");
                    googleLanguage.put("印尼语", "id");
                    googleLanguage.put("英语", "en");
                    googleLanguage.put("越南语", "vi");
                    googleLanguage.put("中文繁体", "zh-TW");
                    googleLanguage.put("中文简体", "zh-CN");
                }
            }
        }
        return googleLanguage;
    }

    /**
     * 对于获取的Google翻译的结果进行解析过滤
     * @param result
     * @return
     */
    public static List<String> parseGoogleResult(String result){
        List<String> translationList = new ArrayList<>(1);
        String[] pattern=result.replace("[", "").split("\",");
        if(pattern.length>0){
            translationList.add(pattern[0].replace("\"",""));
        }
        return translationList;
    }

    private static Map<String, String> baiduLanguage = null;
    public static Map<String, String> getBaiduLanguageList(){
        if(baiduLanguage==null){
            synchronized (Utils.class){
                if(baiduLanguage==null){
                    baiduLanguage=new HashMap<>();
                    baiduLanguage.put("中文","zh");
                    baiduLanguage.put("英语","en");
                    baiduLanguage.put("粤语","yue");
                    baiduLanguage.put("文言文","wyw");
                    baiduLanguage.put("日语","jp");
                    baiduLanguage.put("韩语","kor");
                    baiduLanguage.put("法语","fra");
                    baiduLanguage.put("西班牙语","spa");
                    baiduLanguage.put("泰语","th");
                    baiduLanguage.put("阿拉伯语","ara");
                    baiduLanguage.put("俄语","ru");
                    baiduLanguage.put("葡萄牙语","pt");
                    baiduLanguage.put("德语","de");
                    baiduLanguage.put("意大利语","it");
                    baiduLanguage.put("希腊语","el");
                    baiduLanguage.put("荷兰语","nl");
                    baiduLanguage.put("波兰语","pl");
                    baiduLanguage.put("保加利亚语","bul");
                    baiduLanguage.put("爱沙尼亚语","est");
                    baiduLanguage.put("丹麦语","dan");
                    baiduLanguage.put("芬兰语","fin");
                    baiduLanguage.put("捷克语","cs");
                    baiduLanguage.put("罗马尼亚语","rom");
                    baiduLanguage.put("斯洛文尼亚语","slo");
                    baiduLanguage.put("瑞典语","swe");
                    baiduLanguage.put("匈牙利语","hu");
                    baiduLanguage.put("繁体中文","cht");
                    baiduLanguage.put("越南语","vie");
                }
            }
        }
        return baiduLanguage;
    }

    /**
     * 什么是URL encode？
     * <p>
     * 网络标准RFC 1738规定了URL中只能使用英文字母、阿拉伯数字和某些标点符号，不能使用其他文字和符号。
     * 如果您需要翻译的文本里面出现了不在该规定范围内的字符（比如中文），需要通过URL encode将需要翻译的文本做URL编码才能发送HTTP请求。
     * 大部分编程语言都有现成的URL encode函数，具体使用方法可以针对您使用的编程语言自行搜索。
     *
     * @param text
     * @return
     */
    public static String urlEncode(String text) {
        try {
            Log.d(TAG, "urlEncode: 编码前---->" + text);
            text = URLEncoder.encode(text, "GBK");
            Log.d(TAG, "urlEncode: 编码后---->" + text);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }
        return text;
    }


    /**
     * 获取应用32位签名
     *
     * @param context
     * @return
     */
    public static String getApk32Signature(Context context, String packageName) {

        PackageManager manager = context.getPackageManager();
        /** 通过包管理器获得指定包名包含签名的包信息 **/
        PackageInfo packageInfo = null;
        try {
            packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        /******* 通过返回的包信息获得签名数组 *******/
        Signature[] signatures = packageInfo.signatures;
        String ss = MD5.hexdigest(signatures[0].toByteArray());
        if (ss != null) {
            Toast.makeText(context, "签名" + ss, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "没获得签名", Toast.LENGTH_LONG).show();
        }
        Log.d(TAG, "getApk32Signature: signature--->" + ss);
        return ss;
    }



}
